package com.geekbang.lul.consumer.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.geekbang.lul.consumer.mapper.CustomerBalanceFreezeMapper;
import com.geekbang.lul.consumer.mapper.CustomerMapper;
import com.geekbang.lul.dto.CustomerBalanceFreezeDTO;
import com.geekbang.lul.dto.CustomerDTO;
import com.geekbang.lul.service.consumer.ConsumerCustomerService;
import com.geekbang.lul.service.provider.ProviderCustomerService;
import org.dromara.hmily.annotation.HmilyTCC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author nhsoft.lul
 */
@Service
public class ConsumerCustomerServiceImpl implements ConsumerCustomerService {

    @Reference
    private ProviderCustomerService providerCustomerService;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private CustomerBalanceFreezeMapper customerBalanceFreezeMapper;

    @Override
    @HmilyTCC(confirmMethod = "confirm", cancelMethod = "cancel")
    public boolean trade() {
       //用户a 冻结 1美元
        CustomerDTO a = customerMapper.read(1);

        a.setUsdBalance(a.getUsdBalance().subtract(BigDecimal.ONE));
        if(a.getUsdBalance().compareTo(BigDecimal.ZERO) < 0){
            //余额不足
        }
        customerMapper.updateBalance(a);
        CustomerBalanceFreezeDTO customerBalanceFreezeDTO = new CustomerBalanceFreezeDTO();
        customerBalanceFreezeDTO.setCustomerId(a.getCustomerId());
        customerBalanceFreezeDTO.setCnhFreeze(BigDecimal.ZERO);
        customerBalanceFreezeDTO.setUsdFreeze(BigDecimal.ONE);
        customerBalanceFreezeMapper.save(customerBalanceFreezeDTO);

        // 用户b冻结 7人民币
        CustomerDTO b = providerCustomerService.read(2);
        b.setCnhBalance(b.getUsdBalance().subtract(BigDecimal.valueOf(7)));
        if(b.getUsdBalance().compareTo(BigDecimal.ZERO) < 0){
            //余额不足
        }
        providerCustomerService.updateBalance(b);
        customerBalanceFreezeDTO = new CustomerBalanceFreezeDTO();
        customerBalanceFreezeDTO.setCustomerId(b.getCustomerId());
        customerBalanceFreezeDTO.setUsdFreeze(BigDecimal.ZERO);
        customerBalanceFreezeDTO.setCnhFreeze(BigDecimal.valueOf(7));
        providerCustomerService.saveBalanceFreeze(customerBalanceFreezeDTO);
        return true;
    }

    public boolean confirm() {
        //a 增加 7人民币
        CustomerDTO a = customerMapper.read(1);
        a.setCnhBalance(a.getCnhBalance().add(BigDecimal.valueOf(7)));
        customerMapper.updateBalance(a);

        //b增加1美元
        CustomerDTO b = providerCustomerService.read(2);
        b.setUsdBalance(b.getUsdBalance().subtract(BigDecimal.ONE));
        providerCustomerService.updateBalance(b);

        //删除冻结
        customerBalanceFreezeMapper.delete(1);
        providerCustomerService.deleteBalanceFreeze(1);

        return true;
    }


    public boolean cancel() {
        //a 增加 1美元
        CustomerDTO a = customerMapper.read(1);
        a.setUsdBalance(a.getUsdBalance().add(BigDecimal.ONE));
        customerMapper.updateBalance(a);

        //b增加7人民币
        CustomerDTO b = providerCustomerService.read(2);
        b.setCnhBalance(b.getCnhBalance().add(BigDecimal.valueOf(7)));
        providerCustomerService.updateBalance(b);

        //删除冻结
        customerBalanceFreezeMapper.delete(1);
        providerCustomerService.deleteBalanceFreeze(1);
        return true;
    }
}
